package com.example.timeemail.utils;

import com.example.timeemail.entity.Letter;
import com.google.gson.Gson;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class OkHttpUtil {
    //登陆请求
    public static void loginWithOkHttp(String userName, String email, String password, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();


        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setEmail(email);
        loginRequest.setPassPwd(password);
        loginRequest.setUserName(userName);
        //使用Gson 添加 依赖 compile 'com.google.code.gson:gson:2.8.1'
        Gson gson = new Gson();
        //使用Gson将对象转换为json字符串
        String json = gson.toJson(loginRequest);
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , json);

        Request request = new Request.Builder()
                .url("http://10.0.2.2:8080/openApi/login")
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(callback);
    }

    //注册请求
    public static void registerWithOkHttp(String userName, String email, String password, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setEmail(email);
        loginRequest.setPassPwd(password);
        loginRequest.setUserName(userName);
        //使用Gson 添加 依赖 compile 'com.google.code.gson:gson:2.8.1'
        Gson gson = new Gson();
        //使用Gson将对象转换为json字符串
        String json = gson.toJson(loginRequest);
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , json);

        Request request = new Request.Builder()
                .url("http://10.0.2.2:8080/openApi/register")
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(callback);
    }

    //saveLetter请求
    public static void saveLetterWithOkHttp(Letter letter, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        //使用Gson 添加 依赖 compile 'com.google.code.gson:gson:2.8.1'
        Gson gson = new Gson();
        //使用Gson将对象转换为json字符串
        String json = gson.toJson(letter);
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , json);

        Request request = new Request.Builder()
                .url("http://10.0.2.2:8080/openApi/saveLetter")
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(callback);
    }

    //saveLetter请求
    public static void showLetterWithOkHttp(LoginRequest loginRequest, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        //使用Gson 添加 依赖 compile 'com.google.code.gson:gson:2.8.1'
        Gson gson = new Gson();
        //使用Gson将对象转换为json字符串
        String json = gson.toJson(loginRequest);
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , json);

        Request request = new Request.Builder()
                .url("http://10.0.2.2:8080/openApi/showLetter")
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(callback);
    }
}


